package com.test;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.Loader;
/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @date 2025-6-10
 */
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

public class TextOptimizedPdfCompressor {

    public static void textOptimizedPdfCompressor(String inputPath, String outputPath) throws IOException {
        try (PDDocument document = Loader.loadPDF(new File(inputPath))) {
            int i = 0;
            // 核心优化流程（传递 document 参数）
            for (PDPage page : document.getPages()) {
                i++;
                if (i == 6) {
                    System.out.println(1);
                }
                compressContent(page);
                compressImages(document, page); // ✅ 关键修改：传入 document 对象
            }

            document.save(outputPath);
            System.out.println("✅ 压缩完成！");
        } catch (IOException e) {
            throw e;
        }
    }

    // ================= 内容流压缩 =================
    private static void compressContent(PDPage page) {
        page.getCOSObject().setItem(COSName.FILTER, COSName.FLATE_DECODE);
        page.getCOSObject().setInt(COSName.BITS_PER_COMPONENT, 8);
    }

    // ================= 图片压缩模块 =================
    private static void compressImages(PDDocument document, PDPage page) throws IOException {
        PDResources resources = page.getResources();
        if (resources == null)
            return;

        for (COSName name : resources.getXObjectNames()) {
            if (resources.isImageXObject(name)) {
                PDImageXObject image = (PDImageXObject) resources.getXObject(name);

                // 仅处理大尺寸图片
                if (image.getWidth() * image.getHeight() > 1_000_000) {
                    // ✅ 正确方式：使用传入的 document 参数
                    PDImageXObject compressedImg = JPEGFactory.createFromImage(document, image.getImage(), 0.1f);
                    resources.put(name, compressedImg);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
//        textOptimizedPdfCompressor("d:/opt/教育教学研究论文.pdf", "d:/opt/教育3.pdf");
        textOptimizedPdfCompressor("d:/opt/社保证明+劳动合同.pdf", "d:/opt/社3.pdf");
//        textOptimizedPdfCompressor("d:/opt/论文+查重报告.pdf", "d:/opt/论.pdf");
//        textOptimizedPdfCompressor("d:/opt/陈小琼-广东省中小学教师职称评审申报表.pdf", "d:/opt/陈.pdf");
//        textOptimizedPdfCompressor("d:/opt/工作量证明.pdf", "d:/opt/工.pdf");
//        textOptimizedPdfCompressor("d:/opt/1-5换.pdf", "d:/opt/1-5换1.pdf");
        System.err.println("use time=" + (System.currentTimeMillis() - startTime));
//        System.out.println(1000000 > 1_000_000);
    }
}
